/*** Class Name: WFAssignment 
 * Author: Accenture
 * Date: 13-Jun-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: Select Fulfillment Items from the List view clicks on the WFAssignment button -
                            Assigns workflow to the fulfillment Items based on UDAC,feature code, Region and Transaction Type .
 *                          If no matching WF Group is found, the fulfillment is pushed to WFException Queue.
 **/


public class WFAssignment {

ApexPages.StandardSetController setCon;
list<ID> FIList{get;set;} 
List<Fullfilment_Item__c> fulfillmentList = new List<Fullfilment_Item__c>();
List<Fullfilment_Item__c> wfFulfillmentList = new List<Fullfilment_Item__c>();
List<Fullfilment_Item__c> finalFulfillmentList = new List<Fullfilment_Item__c>();
public String finalURL{get;set;}


public WFAssignment(ApexPages.StandardSetController stdSetController) {
     setCon = stdSetController;
      String returnUrl = ApexPages.currentPage().getParameters().get('retUrl');
      finalURL = URL.getSalesforceBaseUrl().toExternalForm();
      finalURL = finalURL  + '/' + returnUrl; //building the return URL. the control should return to the FI std list view page
     
}

/* 
 * Selected Fulfillment items  from list view will be added to the List FIList. 
 */
public void assignWF(){
     FIList= new list<ID>();
     for(sObject  s :setCon.getSelected() ){ 
        Fullfilment_Item__c FI = (Fullfilment_Item__c)s;
        FIList.add(FI.ID);
     } 
       
     fulfillmentList =[select id,Workflow_Code__c,UDAC__c,Status__c,Feature_Code__c,Transaction_Type__c,Region__c,Ownerid,Related_Case_Id__c,OwnerId__c from Fullfilment_Item__c where id IN :FIList ];
     System.debug('fulfillmentList Size'+fulfillmentList.size()+'fulfillmentList'+fulfillmentList);  
     
     for(Fullfilment_Item__c FI:fulfillmentList){ 
        /*Check to filter out records that are not in exception Queue and items for which WF assignment has already happened  */       
        if(FI.Workflow_Code__c == null && FI.Status__c.equals(IfullfillTextConstant.FI_WORKFLOW_UNASSIGNED_STATUS)){
            wfFulfillmentList.add(FI);
        }
     }
     /*execute the WF assignment logic*/
     if(wfFulfillmentList !=null && wfFulfillmentList.size()>0){
          finalFulfillmentList=iFulfillAssignWorkflow.AssignWorkflow(wfFulfillmentList,true);            
     }
    // System.debug('Final finalFulfillmentList'+finalFulfillmentList);
     if(finalFulfillmentList !=null && finalFulfillmentList.size()>0){
              try{update finalFulfillmentList;}    
              catch(Exception e){GeneralConstants.mailAnyExceptionToMe('iFulfillAssignWorkflow','AssignWorkflow',e );}
     }
      return ;
 
  }

}